package com.bjsxt.usercenter.common.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.alibaba.fastjson.JSON;

public class FileUtil
{
	/**
	 * 读取一个文件
	 * @return
	 */
	public String readFile(File souFile){
		StringBuffer sb = new StringBuffer();
		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(souFile), "UTF-8"));
			String line="";
			while((line=br.readLine())!=null){
				//去掉空格
				line=line.trim();
				if("".equalsIgnoreCase(line)||line.startsWith("//")){
					continue;
				}
				sb.append(line);
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("读取文件失败：文件路径:{}",souFile,e);
		}
		return sb.toString();
	}
	
	/**
	 * 
	 * @param is 输入流
	 * @return
	 */
	public String readFile(InputStream is){
		StringBuffer sb =new StringBuffer();
		try
		{
			BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			String line="";
			while((line=br.readLine())!=null){
				//去掉空格
				line=line.trim();
				if("".equalsIgnoreCase(line)||line.startsWith("//")){
					continue;
				}
				sb.append(line);
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.LOGGER.error("读取文件失败：文件路径:{}",is,e);
		}
		return sb.toString();
	}
	public static void main(String[] args)
	{
		FileUtil fileUtil=new FileUtil();
		File souFile=new File("F:/userCentSrc/userCenter/userCenter_common/src/main/resources/resourceInfo.json");
		String res = fileUtil.readFile(souFile);
		System.out.println(res);
		JSON.parse(res);
	}
}
